package draw;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import mean.field.MFSimulator;

public class XYGraph extends ApplicationFrame {

   
    public XYGraph(final String title) {

        super(title);
        final XYSeries seri1 = new XYSeries("Mean Field Data 1");
        int n = 1000;
        int beta = 2;
        int t = 10; // times
        MFSimulator mfs1 = new MFSimulator(n,beta,t);
        int[] count1 = mfs1.infect();
        for(int i =0;i<count1.length;i++){
        	double x = (double)i/(count1.length-1);
        	double y = (double)count1[i]/n;
        	seri1.add(x,y);
        }
        final XYSeries seri2 = new XYSeries("Mean Field Data 2");
        MFSimulator mfs2 = new MFSimulator(1000,2,10);
        int[] count2 = mfs2.infect();
        for(int i =0;i<count2.length;i++){
        	double x = (double)i/(count2.length-1);
        	double y = (double)count2[i]/n;
        	seri2.add(x,y);
        }
        final XYSeries seri3 = new XYSeries("Mean Field Data 3");
        MFSimulator mfs3 = new MFSimulator(1000,2,10);
        int[] count3 = mfs3.infect();
        for(int i =0;i<count3.length;i++){
        	double x = (double)i/(count3.length-1);
        	double y = (double)count3[i]/n;
        	seri3.add(x,y);
        }
        final XYSeriesCollection data = new XYSeriesCollection();
        data.addSeries(seri1);
        data.addSeries(seri2);
        data.addSeries(seri3);
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Simulation",
            "X", 
            "Y", 
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        XYPlot xyplot = chart.getXYPlot();
        ValueAxis axis = xyplot.getDomainAxis();
        axis = xyplot.getRangeAxis();
        axis.setRange(0,1.2);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 400));
        setContentPane(chartPanel);

    }

    public static void main(final String[] args) {

        final XYGraph demo = new XYGraph("XY Series Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}
